package com.example.demo.test;

import com.example.demo.domain.ScoreSheet;
import com.example.demo.domain.Student;

import java.util.*;
import java.util.stream.Collectors;

public class DataGenerator {
    public static ScoreSheet generateScoreSheet() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 80, 90, 85));
        students.add(new Student("李四", 75, 85, 70));
        students.add(new Student("王五", 90, 80, 95));
        students.add(new Student("张三", 80, 90, 85));
        students.add(new Student("李四", 75, 85, 70));
        students.add(new Student("王五", 90, 80, 95));
        students.add(new Student("张三", 80, 90, 85));
        students.add(new Student("李四", 75, 85, 70));
        students.add(new Student("王五", 90, 80, 95));
        students.add(new Student("张三", 80, 90, 85));
        students.add(new Student("李四", 75, 85, 70));
        students.add(new Student("王五", 90, 80, 95));
        return new ScoreSheet(students);
    }

    public static void main(String[] args) {
        ScoreSheet scoreSheet = generateScoreSheet();
        List<Student> students = scoreSheet.getStudents();
        Map<String, Student> collect = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(Collectors.toList(), list -> {
            Integer math = list.stream().collect(Collectors.summingInt(Student::getMathScore));
            Integer chinese = list.stream().collect(Collectors.summingInt(Student::getChineseScore));
            Integer english = list.stream().collect(Collectors.summingInt(Student::getEnglishScore));
            Student student = new Student(list.get(0).getName(),math,chinese,english);
            return student;
        })));

        collect.forEach((key,values)->{
            System.out.println("key = " + key);
            System.out.println("values ========== " + values );
        });

    }
}